// JavaScript Document
//var size = 0;
var index = -1;

// Pulls data from XML
function loadpage(x){
  index=x;
  document.getElementById('title').innerHTML=getAt("TITLE", x);
	document.getElementById('date').innerHTML= getAt("DATE", x);
	document.getElementById('medium').innerHTML = getAt("MEDIUM", x);
	document.getElementById('awards').innerHTML = getAt("AWARD",x);	
	document.getElementById('comment').innerHTML = getAt("COMMENTS", x);

	document.getElementById('media').innerHTML = getMedia(x);
	
	getThumb();
	
	new MultiBox('mb', {useOverlay: true});
}
function getAt(at,index) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET","database1.xml",false);
	xmlhttp.send(null);
	xmlDoc=xmlhttp.responseXML;
	var x=xmlDoc.getElementsByTagName("PAGE");
	return x[index].getElementsByTagName(at)[0].childNodes[0].nodeValue;
}

// returns the next index integer
function getNextIndex(){
	
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET","database1.xml",false);
	xmlhttp.send(null);
	xmlDoc=xmlhttp.responseXML;
	var x=xmlDoc.getElementsByTagName("PAGE");
	
	if(index + 1 >= x.length){
		document.getElementById('footer').innerHTML= "We've reached the  max:" + index;
		return x.length-1;
	} else {
		index = index + 1;
		document.getElementById('footer').innerHTML= "Index = " + index;
		return index;	
	}
}

// returns the previous index integer
function getPreviousIndex(){
	if(index - 1 < 0){
		document.getElementById('footer').innerHTML= "We've reached the  min:" + index;
		return 0;
	} else {
		index = index - 1;
		document.getElementById('footer').innerHTML= "Index = " + index;
		return index;	
	}
}

function getNextPage(){
	var next = getNextIndex();
	
	document.getElementById('title').innerHTML=getAt("TITLE",next);
	document.getElementById('date').innerHTML= getAt("DATE", next);
	document.getElementById('medium').innerHTML = getAt("MEDIUM",next);
	document.getElementById('awards').innerHTML = getAt("AWARD",next);
	document.getElementById('comment').innerHTML = getAt("COMMENTS",next);
		
	document.getElementById('media').innerHTML = getMedia(next);
	getThumb();
	
	new MultiBox('mb', {useOverlay: true});

}

function getPreviousPage(){
	var prev = getPreviousIndex();
	
	document.getElementById('title').innerHTML=getAt("TITLE",prev);
	document.getElementById('date').innerHTML= getAt("DATE", prev);
	document.getElementById('medium').innerHTML = getAt("MEDIUM",prev);
	document.getElementById('awards').innerHTML = getAt("AWARD",prev);
	document.getElementById('comment').innerHTML = getAt("COMMENTS",prev);

	document.getElementById('media').innerHTML = getMedia(prev);
	
	getThumb();
	
	new MultiBox('mb', {useOverlay: true});
}
function getThumb() {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET","database1.xml",false);
	xmlhttp.send(null);
	xmlDoc=xmlhttp.responseXML;
	var x=xmlDoc.getElementsByTagName("PAGE");
	y="<ul class=\"navlist\">";
	im="image "
	fun="loadpage(";
	list_start = "<li class=\"navbox\">";
	list_end = "</li>";
	
  for(i=0;i<=(x.length-1);i++){
    //r=x[i].getElementsByTagName("THUMB")[0].childNodes[0].nodeValue;
    // y=y + list_start + "<input type="+ im + " src=./run.jpg" + " onclick="+ fun + i + ")" + " >" + list_end;
	y=y + list_start + "<input type="+ im + " src=\"" + getAt("THUMB", i) + "\"" + " onclick="+ fun + i + ")" + " >" + list_end;
  }
  y = y + "</ul> "
  document.getElementById('thumb').innerHTML=y;

  
}

function getMedia(x){
	var type = getAt("TYPE", x);
	var typeOutput = "";
	switch (type) {
		case 'mp3':
			typeOutput = "<a href=\"" + getAt("ORIGINAL",x) +
			"\"rel=\"type:" + getAt("TYPE", x) + "\" class=\"mb\" id=\"omg\" title=\"" + getAt("TITLE",x) + "\">" +
			"\n" +  
			"<img src=\"images/example/music.jpg\" />" +
			"\n" +
			"</a>";
			break;
		case 'jpg':
		case 'gif':
		case 'png':
			typeOutput = "<a href=\"" + getAt("ORIGINAL",x) +
			"\"rel=\"type:" + getAt("TYPE", x) + "\" class=\"mb\" id=\"omg\" title=\"" + getAt("TITLE",x) + "\">" +
			"\n" + 
			"<img src=\"" + getAt("ORIGINAL",x) + "\" />" +
			"\n" +
			"</a>";
			break;
		case 'swf':
			typeOutput = "<a href=\"" + getAt("ORIGINAL",x) +
			"\"rel=\"type:" + getAt("TYPE", x) + "\" class=\"mb\" id=\"omg\" title=\"" + getAt("TITLE",x) + "\">" +
			"\n" +  
			"<img src=\"images/example/movie.jpg\" />" +
			"\n" +
			"</a>";
			break;
		case 'flv':
			typeOutput = "<a href=\"" + getAt("ORIGINAL",x) +
			"\"rel=\"type:" + getAt("TYPE", x) + "\" class=\"mb\" id=\"omg\" title=\"" + getAt("TITLE",x) + "\">" +
			"\n" +  
			"<img src=\"images/example/movie.jpg\" />" +
			"\n" +
			"</a>";
			break;				
		default:
			typeOutput = "<a href=\"" + getAt("ORIGINAL",x) +
			" class=\"mb\" id=\"omg\" title=\"" + getAt("TITLE",x) + "\">" +
			"\n" + 
			"<img src=\"" + getAt("ORIGINAL",x) + "\" />" +
			"\n" +
			"</a>";
	}
	return typeOutput;
}
